package org.fuxia.im.listeners;

import java.util.concurrent.ConcurrentMap;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.fuxia.im.common.ClientConst;
import org.fuxia.im.common.ClientContext;
import org.fuxia.im.common.CommonDialog;
import org.fuxia.im.common.MyMessageDialog;
import org.fuxia.im.common.MyMessageDialog.MessageDialogClickHandler;
import org.fuxia.im.core.entries.ECPacket;
import org.fuxia.im.core.listeners.Listener;
import org.fuxia.im.entities.User;
import org.fuxia.im.gui.Chat;
import org.fuxia.im.gui.GroupChat;

public class ReceiveMessageListener implements Listener {

	@SuppressWarnings("unchecked")
	public void trigger(Object param) {
		final ECPacket packet = (ECPacket) param;
		final String groupId = packet.getParameter("groupId");
		final String msg = packet.getParameter("content");
		final String from = packet.getFrom();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {

				if (packet.getTo().contains("#") || groupId != null) {
					GroupChat gc = ClientContext.getValue(groupId,
							GroupChat.class);
					if (gc != null) {
						gc.insertOtherUserHeader(from);
						gc.insertRecvMessae(msg);
						gc.setState(JFrame.NORMAL);
					} else {

						MyMessageDialog dialog = CommonDialog
								.createMessageNotifyWindow("你有新的消息,发送人【"
										+ from + "】");
						dialog.setClickHandler(new MessageDialogClickHandler() {
							
							public void doAction() {
								GroupChat gc = new GroupChat() ;
								ClientContext.putValue(groupId, gc);
								gc.setState(JFrame.NORMAL) ;
								gc.showGroupChatWindow(groupId);
								gc.insertOtherUserHeader(from);
								gc.insertRecvMessae(msg);
								
							}
						});
						dialog.showDialog();
					}
				} else {

					Chat chatWindow = ClientContext.getValue(from, Chat.class);
					if (chatWindow != null) {
						chatWindow.insertOtherUserHeader(ClientContext
								.findUserByCode(from).getNickName());
						chatWindow.insertRecvMessae(msg);
						chatWindow.toFront();
					} else {
						ConcurrentMap<String, String> map = ClientContext
								.getValue(ClientConst.MESSAGE_QUEUE,
										ConcurrentMap.class);
						map.put(from, msg);

						MyMessageDialog dialog = CommonDialog
								.createMessageNotifyWindow("你有新的消息,发送人【"
										+ from + "】");
						dialog.setClickHandler(new MessageDialogClickHandler() {
							public void doAction() {
								User user = ClientContext.findUserByCode(from);
								Chat chat= ClientContext.getValue(user.getUserCode(),
										Chat.class);
								if (chat == null) {
									chat = new Chat(user);
									ClientContext.putValue(user.getUserCode(), chat);
								}
								chat.setVisible(true);
								chat.insertOtherUserHeader(user.getNickName());
								chat.insertRecvMessae(msg);
								chat.toFront();
							}
						});
						dialog.showDialog();
					}
				}
			}
		});
	}

}
